const app = getApp()
import {
  // reqPaymentInfoList,
  reqWxUserInfo
} from '../../../../../request/api'
Page({
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
    // deviceImg: '/statics/images/device/xiaolaba.png',
    deviceImg: '/statics/images/yjt.png',
    page: 1,
    limit: 10,
    isNextPage: true, //是否下一页
    triggered: false, //是否下拉刷新列表
    dataList: [],
    openId: ""
  },

  onLoad() {
    // app.globalData.appOpenId 未绑定微信的情况下第一次请求时存储起来，防止反复请求
    if (app.globalData.openId || app.globalData.appOpenId) {
      this.setData({
        openId: app.globalData.openId || app.globalData.appOpenId
      })
      // this.getCallHistoryList()
    } else {
      this.getOpenId()
    }
  },
  // 获取微信用户openId
  getOpenId() {
    let _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.login({
      async success(res) {
        if (res.code) {
          const res1 = await reqWxUserInfo({
            jsCode: res.code
          }) //获取信息
          if (res1.code !== 20000) {
            wx.hideLoading()
            showMsg(res1.message)
            return
          }
          console.log(res1, "res1")
          _this.setData({
            openId: res1.data.row.openid
          })
          app.globalData.appOpenId = res1.data.row.openid
          // _this.getCallHistoryList()
        } else {
          wx.hideLoading()
          console.log('绑定失败！')
        }
      }
    })
  },
  async getCallHistoryList() {
    const res = await reqPaymentInfoList(this.data.page, 10, {
      customerOpenId: this.data.openId
    })
    if (res.code != 20000) return Toast.fail(res.message);
    let {
      rows
    } = res.data
    console.log(rows, "rows")
    rows.forEach(item => {
      if (this.timeType(item.content) == "月卡") {
        item.packageType = "月卡"
        item.initPrice = app.globalData.monthPrice
        item.quantity = Math.floor(item.amount / app.globalData.monthPrice)
      } else if (this.timeType(item.content) == "季卡") {
        item.packageType = "季卡"
        item.initPrice = app.globalData.quarterPrice
        item.quantity = Math.floor(item.amount / app.globalData.quarterPrice)
      } else if (this.timeType(item.content) == "年卡") {
        item.packageType = "年卡"
        item.initPrice = app.globalData.yearPrice
        item.quantity = Math.floor(item.amount / app.globalData.yearPrice)
      }
    })
    if (this.data.page === 1) {
      this.setData({
        dataList: rows,
        isNextPage: rows.length >= this.data.limit
      })
      console.log(this.data.dataList, "dataList")
    } else if (rows.length) {
      this.setData({
        dataList: this.data.dataList.concat(rows),
        isNextPage: rows.length >= this.data.limit
      })
    } else {
      this.setData({
        isNextPage: false
      })
    }

  },
  // 套餐类型
  timeType(str) {
    // 正则表达式匹配括号内的内容
    const regex = /\(.*?(月卡|季卡|年卡).*?\)/;
    const match = str.match(regex);

    // 如果找到匹配项，则返回括号内的字符，否则返回null
    return match ? match[0].match(/(月卡|季卡|年卡)/)[0] : null;
  },
  // 滚动到底部触发
  lower() {
    if (!this.data.isNextPage) return
    this.setData({
      page: this.data.page + 1,
    })
    this.getCallHistoryList()

  },
  // 下拉刷新列表
  async onRefresh() {
    this.setData({
      dataList: null,
      page: 1,
      isNextPage: true,
      triggered: true,
    })
    await this.getCallHistoryList()
    this.setData({
      triggered: false,
    })
  },

  // 详情页
  onDetail(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/orderDefine/orderDefine?id=${id}`,
    })
  }
})